//
//  MTMapPackLibrary.m
//  GoMap
//
//  Created by jsheriff on 2/9/11.
//  Copyright 2011 The MITRE Corporation. All rights reserved.
//

#import "MTMapPackLibrary.h"
#import "MTMapPack.h"

@implementation MTMapPackLibrary

@synthesize libraryPath;
@synthesize mapPackList;

-(id)initWithPath:(NSString *)path
{
    if (self = [super init])
    {
		self.libraryPath = path;
		[self refresh];
    }
    return self;
}

-(id)init
{
    return [self initWithPath:nil];
}

- (void)refresh {
	if (mapPackList == nil) {
		mapPackList = [[NSMutableArray alloc] initWithCapacity:1];
	}
	else {
		[mapPackList removeAllObjects];
	}
	
    // Iterate over the documents in the specified path
	if (libraryPath != nil) {
		NSFileManager *manager = [NSFileManager defaultManager];
		NSError *error;
		NSArray *fileList = [manager contentsOfDirectoryAtPath:libraryPath error:&error];
		for (NSString *fileName in fileList) {
			if ([[fileName pathExtension] isEqualToString:@"db"]) {
				NSLog(@"Found map pack: %@", fileName);
				MTMapPack *mapPack = [[MTMapPack alloc] initWithPath:[libraryPath stringByAppendingPathComponent:fileName]];
				[self.mapPackList addObject:mapPack];
				[mapPack release];
			}
		}
	}
}

- (void)dealloc {
	[libraryPath release];
    [mapPackList release];
	
    [super dealloc];
}


@end
